<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>

// 应用上下文:document
// var _documentContext = "<%=request.getContextPath() %>";

// 各业务模块，必须定义的变量（框架issue.js文件中需用到，不要修改变量名）
var _title;	// 业务标题
var _issueId; // 业务主键

var _doSurplusWork = _documentContext + "/supervise/doSuperviseWork";// （流程启动成功后业务数据的相关操作）业务模块自己定义的业务处理方法，以供框架在提交完成后，进行触发

var officeData=new Array();// 正文数据

var saveData=new Array();

var deleteData=new Array();

/**
 * 业务数据初始化
 */
$(function(){
	
	var _issueId=$("#docId").val();
	
	addValid();
});

/**
 * 框架页面调用的保存方法
 */
function _saveOAdata(){
	_title=$("#docName").val();
	
	if(!checkPageItems()){// 页面验证不通过
		return 1;
	}
	var status = submitOAdata();
	if(status != 100)
	{
		return 2;
	}
	return 0;// 0:保存成功，1：验证未通过，2：数据保存错误
}

/**
 * 验证
 */
function checkPageItems(){
	return $("#docSuperviseForm").form('validate') && $("#editTableForm").form('validate');
}

// 保存页面数据
// 返回值100:正常、200:数据保存失败、300:正文及附件保存失败、400:附件上传失败
function submitOAdata(){
	var submitState=0;

	$.ajax({// ajax保存表单基本信息
		   type: "POST",
		   url: "<c:url value='/supervise/saveSupervise' />",
		   data: $('#docSuperviseForm').serialize(),
		   dataType: "json",
		   async:false,
		   success: function(msg){		   
		     if(msg.state=="200"){
		    	 $("#docId").val(msg.docSupervise.docId);
		    	 _issueId=msg.docSupervise.docId;
		    	 if(saveListTableDate(_issueId)==100){
		    			 submitState=100;  
		    	 }else{
		    		 submitState=400;
		    	 }	    	 
		     }else{
		    	 submitState=200;
		     }
		   }
		});
	return submitState;
}
function saveListTableDate(superviseId){
	var submitState= 0;
	saveData.length = 0;
    $('#listTable').find('.cloneTr').each(function(i,n){
    	var obj = new Object();
    	obj.id=$(n).find("input[name='id']").val();
    	obj.itemName=$(n).find("input[name='itemName']").val();
    	obj.underTakeDept=$(n).find("input[name='underTakeDept']").val();
    	
    	obj.underTakenCode="";
    	$(n).find("input[name='underTakenCode']").each(function(j,m){
    		obj.underTakenCode+=(obj.underTakenCode==""?"":",")+$(m).val();
    	});
    	obj.limitTime=$(n).find("input[name='limitTime']").val();
    	obj.superviseId=$("#docId").val();
    	obj.bak1=$(n).find("input[name='ctId']").val();
        saveData.push(obj);
    });
    $.ajax({// ajax保存表单基本信息
		   type: "POST",
		   url: "<c:url value='/supervise/saveSuperviseAsso' />",
		   data: "paramStr="+encodeURIComponent(encodeURIComponent(JSON.stringify(saveData)))
			+"&delIds="+JSON.stringify(deleteData),
		   dataType: "json",
		   async:false,
		   success: function(msg){
			   if(msg.status=="200"){
				   $.each(msg.data,function(i,n){
					   $('#listTable').find('.cloneTr').each(function(j,m){
						   if(n.bak1==$(m).find("input[name='ctId']").val()){
							   $(m).find("input[name='id']").val(n.id);
							   return false;
						   }
					   });
				   });
				   deleteData.length=0;
				   submitState =  100;
			   }
	},error:function(msg){
	    return submitState = 400;
	}
    });    
    return submitState;
}
// 绑定验证
function addValid(){
	$("#batchDownloadFile").hide();
	var isNewFlag=_workEffortId=="isNew";
	// 标题
	$("#docName").textbox({
	    required: true,
	    validType:'length[1,100]',
	    editable:isNewFlag
	});
	$("#superviseSource").combobox({
		required: true,
		validType:"selectValueRequired",
		editable:false,
 		disabled:!isNewFlag
	});
	
	$("#listTable").find("input[name='itemName']").textbox({
   	 	required: true,
   		editable: isNewFlag,
		validType:'length[1,65]'
  });
	$("#listTable").find("select[name='underTakenCode']").combobox({
		editable:false,
		disabled:!isNewFlag,
		required: true,
	    separator:",",
 		multiple:true,
 		formatter:function(row){ 
 			var ctId=$(this).parents("td").find("input[name='ctId']").val();
	    	return '<input id="'+row.value+ctId+'" type="checkbox" value="'+row.value+'"><span for="'+row.value+ctId+'">'+row.text+'</span>'; 
	    },
	    onSelect:function(record){
	    	if(record.value!=""){
	    		var ctId=$(this).parents("td").find("input[name='ctId']").val();
	    		$("input[id='"+record.value+ctId+"']").prop("checked",true);
	    	}
	    },
	    onUnselect:function(record){
	    	if(record.value!=""){
	    		var ctId=$(this).parents("td").find("input[name='ctId']").val();
	    		$("input[id='"+record.value+ctId+"']").prop("checked",false);
	    	}
	    },
	    onChange: function(record,recorddel){
	    	var text = $(this).combobox("getText");
    		$(this).parent("td").find("input[name='underTakeDept']").val(text); 		
	    },
	    onLoadSuccess:function(){
	    	var orgCodes=$(this).parent("td").find("input[name='orgCodes']").val().split(",");
	    	$(this).combobox('setValues', orgCodes);
	    }
 });
	$("#listTable").find("input[name='itemName']").textbox({
	   	 required: true,
	   	 disabled:!isNewFlag,
		validType:'length[1,65]'
    });
	
	$("#listTable").find("input[name='limitTime']").datebox({
    	required: true,
    	disabled:!isNewFlag,
 		editable:false
     });
	$("#listTable").find("a[name='clearbutton']").linkbutton({disabled:!isNewFlag});
	
	if(isNewFlag){
		$("#displayNumberImg").show();
		$("#displayNumberB").prop("disabled",false);
		if(_issueType=="todo" && $("#listTable").find(".cloneTr").size()==0){
			addtr();
		}
	}else{
		$("#addButtonDiv").hide();
	}
}

function removetr(obj){
	  if($(obj).parents("tr").find("input[name='id']").val()!=""){
		  deleteData.push($(obj).parents("tr").find("input[name='id']").val());
	  }
	$.each( $(obj).parents("tr").nextAll(), function(i, n){
		  $(n).find("span[name='index']").text($(n).find("span[name='index']").text()-1);
	});
	$(obj).parents("tr").remove();
}

// 表格动态加载行
function addtr(){
	var temp=new Date().getTime();
	var trHTML =$("#cloneTr").clone().removeAttr("id").show();
	trHTML.find("input[name='ctId']").val(temp);
	 trHTML.find("span[name='index']").text($("#listTable").find("tr").size()-1);
     trHTML.find("input[name='limitTime']").datebox({
    	required: true,
 		editable:false
     });
     trHTML.find("select[name='underTakenCode']").combobox({
    		editable:false,
    		required: true,
    	    separator:",",
     		multiple:true,
     		formatter:function(row){ 
    	    	return '<input id="'+row.value+temp+'" type="checkbox" value="'+row.value+'"><span for="'+row.value+temp+'">'+row.text+'</span>'; 
    	    },
    	    onSelect:function(record){
    	    	if(record.value!=""){
    	    		var ctId=$(this).parents("td").find("input[name='ctId']").val();
    	    		$("input[id='"+record.value+ctId+"']").prop("checked",true);
    	    	}
    	    },
    	    onUnselect:function(record){
    	    	if(record.value!=""){
    	    		var ctId=$(this).parents("td").find("input[name='ctId']").val();
    	    		$("input[id='"+record.value+ctId+"']").prop("checked",false);
    	    	}
    	    },
    	    onChange: function(record,recorddel){
    	    	var text = $(this).combobox("getText");
        		$(this).parent("td").find("input[name='underTakeDept']").val(text); 		
    	    }
     }).combobox("clear");
	         trHTML.find("input[name='itemName']").textbox({
	        	 required: true,
	     		validType:'length[1,65]'
	         });
	         trHTML.find("a[name='clearbutton']").linkbutton({});
	
	        
	         $("#listTable").append(trHTML);
}